package com.share.device.controller;

import java.util.List;
import java.io.IOException;

import com.share.device.domain.CabinetSlot;
import com.share.device.domain.PowerBank;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.device.domain.Cabinet;
import com.share.device.service.ICabinetService;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.utils.poi.ExcelUtil;
import com.share.common.core.web.page.TableDataInfo;

/**
 * 充电宝柜机Controller
 *
 * @author ruoyi
 * @date 2025-06-24
 */
@RestController
@RequestMapping("/cabinet")
public class CabinetController extends BaseController
{
    @Autowired
    private ICabinetService cabinetService;

    /**
     * 查询充电宝柜机列表
     */
    @RequiresPermissions("device:cabinet:list")
    @GetMapping("/list")
    public TableDataInfo list(Cabinet cabinet)
    {
        startPage();
        List<Cabinet> list = cabinetService.selectCabinetList(cabinet);
        return getDataTable(list);
    }


    //获取充电宝下拉框
    @GetMapping("/getPowerBankList")
    public AjaxResult getPowerBankList(){
      List<PowerBank> powerBankList =  cabinetService.getPowerBankList();
      return AjaxResult.success(powerBankList);
    }

    //添加插槽表
    @PostMapping("/addSlot")
    public AjaxResult addSlot(@RequestBody CabinetSlot cabinetSlot){
        return AjaxResult.success(cabinetService.addSlot(cabinetSlot));
    }

    //查询未删除且启用的插槽
    @GetMapping("/getSlot/{cabinetId}")
    public AjaxResult getCabinetSlots(@PathVariable Integer cabinetId) {
        List<CabinetSlot> slots = cabinetService.getSlotsByCabinetId(cabinetId);
        return AjaxResult.success(slots); // 返回查询结果
    }



    /**
     * 导出充电宝柜机列表
     */
    @RequiresPermissions("device:cabinet:export")
    @Log(title = "充电宝柜机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cabinet cabinet)
    {
        List<Cabinet> list = cabinetService.selectCabinetList(cabinet);
        ExcelUtil<Cabinet> util = new ExcelUtil<Cabinet>(Cabinet.class);
        util.exportExcel(response, list, "充电宝柜机数据");
    }

    /**
     * 获取充电宝柜机详细信息
     */
    @RequiresPermissions("device:cabinet:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cabinetService.selectCabinetById(id));
    }

    /**
     * 新增充电宝柜机
     */
    @RequiresPermissions("device:cabinet:add")
    @Log(title = "充电宝柜机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cabinet cabinet)
    {
        return toAjax(cabinetService.insertCabinet(cabinet));
    }

    /**
     * 修改充电宝柜机
     */
    @RequiresPermissions("device:cabinet:edit")
    @Log(title = "充电宝柜机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cabinet cabinet)
    {
        return toAjax(cabinetService.updateCabinet(cabinet));
    }

    /**
     * 删除充电宝柜机
     */
    @RequiresPermissions("device:cabinet:remove")
    @Log(title = "充电宝柜机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cabinetService.deleteCabinetByIds(ids));
    }
}
